﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using Lon.Util;
using System.Net;

namespace Lon.Server
{
    public partial class FormDataShowNew : Form
    {
        #region 字符串常量
      
        //Button
        public const string ID_CLEAR = "清空";
        public const string ID_PAUSE = "暂停";
        public const string ID_CONTINUE = "继续";

     
        //ViewData
        public const string ID_TIME = "时间";
        public const string ID_DIR = "方向";
        public const string ID_DST = "目的电报码";
        public const string ID_SRC = "源电报码";
        public const string ID_MAJOR_TYPE = "类型码";
        public const string ID_MINOR_TYPE = "子类型码";
        public const string ID_MAJOR_TYPE_DESP = "类型描述";
        public const string ID_DATA = "数据";

        #endregion
        /// <summary>
        /// 当前查看的数据节点
        /// </summary>
        private NetNode CurrentViewNetNode = null;
  
        public FormDataShowNew(NetNode node)
        {  
           
            InitializeComponent();
            this.CurrentViewNetNode = node;
         
        }
        protected override void OnLoad(EventArgs e)
        {
            this.btnClear.Text = ID_CLEAR;
            this.btnPause.Text = ID_PAUSE;
            InitDataGrid();
            ;




            this.Text = String.Format("数据查看[{0}],{1}:{2}", CurrentViewNetNode.Dbm, CurrentViewNetNode.Ip, CurrentViewNetNode.NodeType);
       
  
            base.OnLoad(e);
        }
        Queue<NetRawDataRec> rawDataQuene = new Queue<NetRawDataRec>();
        void CurrentViewNetNode_DataUpdateHandler(object sender, RawDataUpdateEventArgs e)
        {
            if (pause) return;
            var rec = e.Rec;
            lock (rawDataQuene)
            {
                rawDataQuene.Enqueue(rec);
            }
        }
      
        DataTable dtViewData = new DataTable();
        BindingSource bsViewData = new BindingSource();
        DataFilter filterViewData;
        void InitDataGrid()
        {
            SetTagValue();

            //------------------------------------------


            //------------------------------------------

            filterViewData = new DataFilter();

            dataGridView2.Columns.Clear();
            dtViewData.Columns.Add(ID_TIME);
            dtViewData.Columns.Add(ID_DIR);
            dtViewData.Columns.Add(ID_DST);
            dtViewData.Columns.Add(ID_SRC);
            dtViewData.Columns.Add(ID_MAJOR_TYPE);
            dtViewData.Columns.Add(ID_MINOR_TYPE);
            dtViewData.Columns.Add(ID_MAJOR_TYPE_DESP);
            dtViewData.Columns.Add(ID_DATA);

            bsViewData.DataSource = dtViewData;
            dataGridView2.DataSource = bsViewData;
            dataGridView2.Columns[ID_DIR].Width = 40;
            dataGridView2.Columns[ID_DST].Width = 40;
            dataGridView2.Columns[ID_SRC].Width = 40;
            dataGridView2.Columns[ID_MAJOR_TYPE].Width = 40;
            dataGridView2.Columns[ID_MINOR_TYPE].Width = 40;
            dataGridView2.Columns[dataGridView2.Columns.Count - 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;

           this.CurrentViewNetNode.DataUpdateHandler += new EventHandler<RawDataUpdateEventArgs>(CurrentViewNetNode_DataUpdateHandler);
            this.dataGridView2.SelectionChanged+=new EventHandler(dataGridView2_SelectionChanged);
        }

        private void btnClear_Click(object sender, EventArgs e)
        {
            dataGridView2.Rows.Clear();
            txtDetail.Clear();
        }

        bool pause = false;
        private void btnPause_Click(object sender, EventArgs e)
        {

        }

   
        void SetTagValue()
        {

            txtDest.Tag = ID_DST;
            txtSrc.Tag = ID_SRC;
            txtMajorType.Tag = ID_MAJOR_TYPE;
            txtMinorType.Tag = ID_MINOR_TYPE;

        }

        private void dataGridView2_SelectionChanged(object sender, EventArgs e)
        {
            //网络数据列表
            DataGridView dgv = (DataGridView)sender;

            if (dgv.SelectedRows.Count > 0)
            {
                txtDetail.Text = dgv.SelectedRows[0].Cells[ID_DATA].Value.ToString();
            }

        }

        void UpdateViewData()
        {
            try
            {
                if (pause) return;
                if (this.CurrentViewNetNode == null)
                {
                    return;
                }
                NetRawDataRec[] recs = null;
                lock (rawDataQuene)
                {
                    recs = rawDataQuene.ToArray();
                    rawDataQuene.Clear();
                }
                if (recs == null || recs.Length == 0)
                {
                    return;
                }
                ShowRawData(recs);



            }
            catch (Exception ex)
            {
            }
        }

        private void ShowRawData(NetRawDataRec[] recs)
        {
            for (int i = 0; i < recs.Length; i++)
            {
                var item = recs[i];
                DataRow row = dtViewData.NewRow();
                row[ID_TIME] = item.Time.ToString("HH:mm:ss.fff");
                row[ID_DIR] = EnumHelper.GetEnumDisString(item.DataDir.GetType(), (int)item.DataDir);
                row[ID_DST] = item.DstDbm;
                row[ID_SRC] = item.SrcDbm;
                row[ID_MAJOR_TYPE] = ((int)item.MajorDataType).ToString("X");
                row[ID_MINOR_TYPE] = item.MinorDataType.ToString("X2");
                row[ID_MAJOR_TYPE_DESP] = item.MajorDataTypeText;
                row[ID_DATA] = item.DataStr;
                dtViewData.Rows.Add(row);
                //dtViewData.Rows.InsertAt(row, 0);  
                if (dtViewData.Rows.Count > 2000)
                {
                    dtViewData.Rows.RemoveAt(0);
                }
            }

        }


        void ViewData_TextChanged(object sender, EventArgs e)
        {
            TextBox textBox = (TextBox)sender;
            string text = textBox.Text;
            string key = textBox.Tag.ToString();

            filterViewData.Set(key, text);
            bsViewData.Filter = filterViewData.GetText();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            UpdateViewData();
        }
    }
}
